package com.example.javabasic.suanfa.jiandan.栈与队列;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * @description 队列实现栈
 * @Author Lin FuYuan
 * @date 2021/5/19
 */
public class QueueOfStack {
    private Queue<Integer> qA = new ArrayDeque<>();
    private Queue<Integer> qB = new ArrayDeque<>();
    //A队列是否为空
    private boolean isA = true;

    public QueueOfStack(){

    }

    public int pop(){
        if(isA){
            return qB.poll();
        }else{
            return qA.poll();
        }
    }


    public void push(int x){
        if(isA){
            qA.offer(x);
            while(!qB.isEmpty()){
                qA.offer(qB.poll());
            }
            isA = false;
        }else{
            qB.offer(x);
            while(!qA.isEmpty()){
                qB.offer(qA.poll());
            }
            isA = true;
        }
    }

    public int top(){
        if(isA){
            return qB.peek();
        }else{
            return qA.peek();
        }
    }


    public boolean empty(){
        if(qA.isEmpty()&& qB.isEmpty()){
            return true;
        }
        return false;
    }
}
